package com.spddr.pmos.cxf.impl;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spddr.pmos.cxf.JaxrsPowerGridPlanForPlant;
import com.spddr.pmos.jpa.PowerGridPlanForPlant;
import com.spddr.pmos.repos.PowerGridPlanForPlantRepos;

@Service
public class JaxrsPowerGridPlanForPlantImpl  implements JaxrsPowerGridPlanForPlant {
	@Autowired
	private PowerGridPlanForPlantRepos repository;
	@Override
	public JpaRepository<PowerGridPlanForPlant, Long> getRepository() {
		// TODO Auto-generated method stub
		return repository;
	}
	@Override
	public Integer saveall(String param) {
		String newParam="";
		if(null!=param&&!param.equals("")){
			newParam = param.replace("^", "[");
			newParam = newParam.replace("*", "]");
		}
		ObjectMapper mapper = new ObjectMapper();
		
		try {
			List<PowerGridPlanForPlant> lendReco = (List<PowerGridPlanForPlant>)mapper.readValue(newParam,new TypeReference<List<PowerGridPlanForPlant>>() { });
			if(lendReco.size()>0){
				for(PowerGridPlanForPlant mm:lendReco){
					mm.setDisable(1);
					repository.save(mm);
				}
			}
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
		
		System.out.println(param);
		return 1;
	}
	@Override
	public List<PowerGridPlanForPlant> findAll(Long id) throws ParseException {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//		if(null!=datadate&&datadate.equals("")){
//			Date date = sdf.parse(datadate);
			return repository.findByOrgIdAndDisable(id,1);
//		}
//		return null;
	}

	

}
